
import 'package:flutter/material.dart';
import 'package:mxapp/home/movie.dart';
import 'CustomSwiper.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:mxapp/utils/global.dart';
import 'package:flutter_staggered_animations/flutter_staggered_animations.dart';


class GoodsList extends StatefulWidget {
  @override
  _GoodsListState createState() => _GoodsListState();
}

class _GoodsListState extends State<GoodsList> {
  List list = [];

  @override
  void initState() {
    super.initState();
    _pullNet();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
          appBar: AppBar(
            title: Text('鹰唯匿'),
            elevation: 0,
          ),
          body: CustomScrollView(
            slivers: <Widget>[
              /* const SliverAppBar(
                pinned: true,
                expandedHeight: 250.0,
                flexibleSpace: FlexibleSpaceBar(
                  title: Text('Demo'),
                ),
              ),*/
              SliverToBoxAdapter(
                child: Container(
                  margin: EdgeInsets.all(5),
                  height: 120,
                  child: CustomSwiper(),
                ),
              ),
              SliverGrid.count(
                crossAxisCount: 6,
                children: buildGridTileList(10),
              ),
              SliverToBoxAdapter(
                child: Container(
                  margin: EdgeInsets.all(5),
                  height: 60,
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(15),
                      color: Colors.black38,
                      image: DecorationImage(
                          image: NetworkImage(
                              'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3276446183,4258702571&fm=26&gp=0.jpg'),
                          fit: BoxFit.fill)),
                  child: InkWell(
                    onTap: () {
                      print('您点击了广告');
                    },
                  ),
                ),
              ),
              /*SliverAnimatedList(
                    initialItemCount: 10,
                      itemBuilder: (BuildContext context, int index, Animation<double> animation){
                        return AnimationConfiguration.staggeredGrid(
                          columnCount: 20,
                          position: index,
                          duration: Duration(milliseconds: 1000),
                          child: SlideAnimation(
                             verticalOffset: 100,
                              child: FadeInAnimation(child: Container(
                                width: 20,
                                height: 20,
                                color: Colors.blue,
                              ))
                          ),
                        );
                      }
                  ),*/
              SliverGrid.count(
                childAspectRatio: 1.0,
                crossAxisCount: 2,
                children: movieList(),
              ),
              /*SliverFixedExtentList(
                    itemExtent: 50.0,
                    delegate: SliverChildBuilderDelegate(
                      (BuildContext context, int index) {
                        return Container(
                          alignment: Alignment.center,
                          color: Colors.lightBlue[100 * (index % 9)],
                          child: Text('list item $index'),
                        );
                      },
                    ),
                  ),*/
            ],
          ),
        );
  }

  List<String> title = [
    "金币",
    "蚂蚁庄园",
    "充值中心",
    "花呗",
    "借呗",
    "饿了么",
    "美团",
    "京东",
    "付款",
    "出行",
    "出行",
    "出行"
  ];
  List<Icon> icon = [
    Icon(
      Icons.monetization_on,
      color: Colors.amber,
      size: 30,
    ),
    Icon(
      Icons.face,
      color: Colors.deepOrangeAccent,
      size: 30,
    ),
    Icon(
      Icons.ac_unit,
      color: Colors.greenAccent,
      size: 30,
    ),
    Icon(
      Icons.favorite,
      color: Colors.red,
      size: 30,
    ),
    Icon(
      Icons.settings,
      color: Colors.black,
      size: 30,
    ),
    Icon(
      Icons.account_circle,
      color: Colors.blueAccent,
      size: 30,
    ),
    Icon(
      Icons.backup,
      color: Colors.lightGreen,
      size: 30,
    ),
    Icon(
      Icons.business_center,
      color: Colors.purple,
      size: 30,
    ),
    Icon(
      Icons.child_friendly,
      color: Colors.green,
      size: 30,
    ),
    Icon(
      Icons.local_movies,
      color: Colors.black38,
      size: 30,
    ),
    Icon(
      Icons.add_to_queue,
      color: Colors.deepPurpleAccent,
      size: 30,
    ),
    Icon(
      Icons.add_to_queue,
      color: Colors.deepPurpleAccent,
      size: 30,
    ),
  ];

  void _pullNet() async{
    await http.get('https://haokan.baidu.com/videoui/api/videorec?tab=yinyue&act=pcFeed&pd=pc&num=30').
    then((http.Response response){
      Map<String, dynamic> user = json.decode(response.body);
      setState(() {
          list = user['data']['response']['videos'];
      });
    });

  }

  List<Widget> movieList() {
    List<Widget> widgetList = new List();
    if(list.length>0){
      for (int i = 0; i < list.length; i++) {
        widgetList.add(Movie(title:list[i]['title'],sourceName:list[i]['source_name'],
          posterSmall:list[i]['poster_small'],fmlike: list[i]['fmlike'],fmplaycnt: list[i]['fmplaycnt_2'],
          duration: list[i]['duration'],
        ));
      }
    }
    return widgetList;
  }

  List<Widget> buildGridTileList(int number) {
    List<Widget> widgetList = new List();
    for (int i = 0; i < number; i++) {
      widgetList.add(
        InkWell(
          splashColor: Colors.transparent,
          highlightColor: Colors.transparent,
          onTap: () {
            print('index');
          },
          child: Column(
            mainAxisSize: MainAxisSize.max,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              icon[i],
              Text(
                title[i],
                style: TextStyle(
                    color: Colors.black87,
                    fontWeight: FontWeight.w300,
                    fontSize: 12),
              )
            ],
          ),
        ),
      );
    }
    return widgetList;
  }
}
